package service

import (
	"bloggo/structs"
	"context"
	"errors"
	"fmt"
	"gitee.com/chunanyong/logger"
	"gitee.com/chunanyong/zorm"
)

//SaveTbPhotoAlbumStruct 保存 相册
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func SaveTbPhotoAlbumStruct(ctx context.Context, tbphotoalbumstruct *structs.TbPhotoAlbumStruct) error {

	// tbphotoalbumstruct对象指针不能为空
	if tbphotoalbumstruct == nil {
		return errors.New("tbphotoalbumstruct对象指针不能为空")
	}
	//匿名函数return的error如果不为nil,事务就会回滚
	_, errSaveTbPhotoAlbumStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始

		_, errSaveTbPhotoAlbumStruct := zorm.Insert(ctx, tbphotoalbumstruct)

		if errSaveTbPhotoAlbumStruct != nil {
			return nil, errSaveTbPhotoAlbumStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errSaveTbPhotoAlbumStruct != nil {
		errSaveTbPhotoAlbumStruct := fmt.Errorf("service.SaveTbPhotoAlbumStruct错误:%w", errSaveTbPhotoAlbumStruct)
		logger.Error(errSaveTbPhotoAlbumStruct)
		return errSaveTbPhotoAlbumStruct
	}

	return nil
}

//UpdateTbPhotoAlbumStruct 更新相册
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateTbPhotoAlbumStruct(ctx context.Context, tbphotoalbumstruct *structs.TbPhotoAlbumStruct) error {

	// tbphotoalbumstruct对象指针或主键Id不能为空
	if tbphotoalbumstruct == nil || (tbphotoalbumstruct.Id) < 1 {
		return errors.New("tbphotoalbumstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbPhotoAlbumStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbPhotoAlbumStruct := zorm.Update(ctx, tbphotoalbumstruct)

		if errUpdateTbPhotoAlbumStruct != nil {
			return nil, errUpdateTbPhotoAlbumStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbPhotoAlbumStruct != nil {
		errUpdateTbPhotoAlbumStruct := fmt.Errorf("service.UpdateTbPhotoAlbumStruct错误:%w", errUpdateTbPhotoAlbumStruct)
		logger.Error(errUpdateTbPhotoAlbumStruct)
		return errUpdateTbPhotoAlbumStruct
	}

	return nil
}

//UpdateNotZeroTbPhotoAlbumStruct 更新相册
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateNotZeroTbPhotoAlbumStruct(ctx context.Context, tbphotoalbumstruct *structs.TbPhotoAlbumStruct) error {

	// tbphotoalbumstruct对象指针或主键Id不能为空
	if tbphotoalbumstruct == nil || (tbphotoalbumstruct.Id) < 1 {
		return errors.New("tbphotoalbumstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbPhotoAlbumStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbPhotoAlbumStruct := zorm.UpdateNotZeroValue(ctx, tbphotoalbumstruct)

		if errUpdateTbPhotoAlbumStruct != nil {
			return nil, errUpdateTbPhotoAlbumStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbPhotoAlbumStruct != nil {
		errUpdateTbPhotoAlbumStruct := fmt.Errorf("service.UpdateNotZeroTbPhotoAlbumStruct错误:%w", errUpdateTbPhotoAlbumStruct)
		logger.Error(errUpdateTbPhotoAlbumStruct)
		return errUpdateTbPhotoAlbumStruct
	}

	return nil
}

//DeleteTbPhotoAlbumStructById 根据Id删除相册
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func DeleteTbPhotoAlbumStructById(ctx context.Context, id string) error {

	//id不能为空
	if len(id) < 1 {
		return errors.New("id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errDeleteTbPhotoAlbumStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		finder := zorm.NewDeleteFinder(structs.TbPhotoAlbumStructTableName).Append(" WHERE id=?", id)
		_, errDeleteTbPhotoAlbumStruct := zorm.UpdateFinder(ctx, finder)

		if errDeleteTbPhotoAlbumStruct != nil {
			return nil, errDeleteTbPhotoAlbumStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errDeleteTbPhotoAlbumStruct != nil {
		errDeleteTbPhotoAlbumStruct := fmt.Errorf("service.DeleteTbPhotoAlbumStruct错误:%w", errDeleteTbPhotoAlbumStruct)
		logger.Error(errDeleteTbPhotoAlbumStruct)
		return errDeleteTbPhotoAlbumStruct
	}

	return nil
}

//FindTbPhotoAlbumStructById 根据Id查询相册信息
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbPhotoAlbumStructById(ctx context.Context, id string) (*structs.TbPhotoAlbumStruct, error) {
	//id不能为空
	if len(id) < 1 {
		return nil, errors.New("id不能为空")
	}

	//根据Id查询
	finder := zorm.NewSelectFinder(structs.TbPhotoAlbumStructTableName).Append(" WHERE id=?", id)
	tbphotoalbumstruct := structs.TbPhotoAlbumStruct{}
	has, errFindTbPhotoAlbumStructById := zorm.QueryRow(ctx, finder, &tbphotoalbumstruct)

	//记录错误
	if errFindTbPhotoAlbumStructById != nil {
		errFindTbPhotoAlbumStructById := fmt.Errorf("service.FindTbPhotoAlbumStructById错误:%w", errFindTbPhotoAlbumStructById)
		logger.Error(errFindTbPhotoAlbumStructById)
		return nil, errFindTbPhotoAlbumStructById
	}

	if !has {
		return nil, nil
	}

	return &tbphotoalbumstruct, nil

}

//FindTbPhotoAlbumStructList 根据Finder查询相册列表
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbPhotoAlbumStructList(ctx context.Context, finder *zorm.Finder, page *zorm.Page) ([]structs.TbPhotoAlbumStruct, error) {

	//finder不能为空
	if finder == nil {
		return nil, errors.New("finder不能为空")
	}

	tbphotoalbumstructList := make([]structs.TbPhotoAlbumStruct, 0)
	errFindTbPhotoAlbumStructList := zorm.Query(ctx, finder, &tbphotoalbumstructList, page)

	//记录错误
	if errFindTbPhotoAlbumStructList != nil {
		errFindTbPhotoAlbumStructList := fmt.Errorf("service.FindTbPhotoAlbumStructList错误:%w", errFindTbPhotoAlbumStructList)
		logger.Error(errFindTbPhotoAlbumStructList)
		return nil, errFindTbPhotoAlbumStructList
	}

	return tbphotoalbumstructList, nil
}
